/*
 * Package il.ac.biu.cs.grossmm.test.test2
 * File FooToPidf.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.test3;

import static il.ac.biu.cs.grossmm.api.data.NodeTypeByInterface.*;
import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.*;
import static il.ac.biu.cs.grossmm.api.presence.BaseVocabulary.*;
import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.flow.ConditionalConverterBase;
import il.ac.biu.cs.grossmm.api.flow.ConverterProxy;
import il.ac.biu.cs.grossmm.api.flow.NodeEventInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeManipulator;

import java.util.logging.Level;
import java.util.logging.Logger;

public class BarToFoo extends
		ConditionalConverterBase<BarPresence, FooPresence> {

	Logger logger = Logger.getLogger("BarToFoo");

	public BarToFoo() {
		super("Bar To Presence Converter", nodeType(BarPresence.class),
				maskPattern(mandatory(CONTENT_TYPE, "application/bar+pidf")),
				nodeType(FooPresence.class), maskPattern(), 0.8d);
	}

	static class Proxy implements ConverterProxy<BarPresence, FooPresence> {
		Logger logger = Logger.getLogger("FooToPidf.Proxy");

		Root<BarPresence> bar;

		Root<FooPresence> fooPresence;

		public Proxy(Root<BarPresence> srcNode, Root<FooPresence> dstNode,
				NodeInspector srcInspector, NodeManipulator dstManipulator)
				throws OperationFailedException {
			this.fooPresence = dstNode;
			this.bar = srcNode;
			convert(srcInspector, dstManipulator);
		}

		public void convert(NodeEventInspector srcInspector,
				NodeManipulator dstManipulator) throws OperationFailedException {
			convert((NodeInspector) srcInspector, dstManipulator);
		}

		/**
		 * @param srcInspector
		 * @param dstManipulator
		 * @throws OperationFailedException
		 */
		private void convert(NodeInspector srcInspector,
				NodeManipulator dstManipulator) throws OperationFailedException {
			String entity = srcInspector.getValue(bar, BarPresence.NAME);
			boolean online = srcInspector.getValue(bar, BarPresence.AVAILABLE);
			dstManipulator.setValue(fooPresence, FooPresence.ENTITY, entity);
			dstManipulator.setValue(fooPresence, FooPresence.IS_ONLINE, online);

			if (logger.isLoggable(Level.FINE))
				logger.fine("Converted " + srcInspector.toString(bar) + " TO "
						+ dstManipulator.toString(fooPresence));
		}

		public void release() {

		}

	}

	public ConverterProxy<BarPresence, FooPresence> createProxy(
			Root<BarPresence> srcNode, Root<FooPresence> dstNode,
			NodeInspector srcInspector, NodeManipulator dstManipulator)
			throws OperationFailedException {
		// logger.info("Proxy created");

		return new Proxy(srcNode, dstNode, srcInspector, dstManipulator);
	}
}
